<?php include("head.php"); ?>
    <div class="sui-layout">
		  <div class="sidebar">
<?php include("leftmenu.php"); ?>	  	
		  </div>
		  <div class="content">
			<p class="sui-text-xxlarge my-padd">课程列表</p>
            <!-- 所有课程的列表 -->
            <table class="sui-table table-primary">
                <tr><th>课程编号</th><th>课程名称</th><th>学习时长</th><th>操作</th></tr>
                <?php
                    include("conn.php");
                    //执行sql语句
                    //分页器所有用到的数据
                    $pagesize = 5;//约定每页显示5条
                    $pagenum = empty($_REQUEST['pagenum'])?1:$_REQUEST['pagenum'];//第几页
                    $allnums = getAll();
                    $maxpage = ceil( $allnums/$pagesize );
                    //将每页记录转换成数组
                    function page($pagenum,$pagesize){
                        global $conn;
                        $sql = "select 课程编号,课程名称,学习时长 from 课程 limit ".($pagenum-1)*$pagesize.",{$pagesize}";
                        $result = mysqli_query($conn,$sql);
                        $allnews = array();
                        if( mysqli_num_rows($result)>0 ){
                            while ($arr = mysqli_fetch_assoc($result)){
                                $allnews[] = $arr;
                            }
                        }
                        return $allnews;
                    }
                    //获取总的记录条数
                    function getAll(){
                        global $conn;
                         $sql1 = "select count(*) as 总数 from 课程";
                         $result = mysqli_query($conn,$sql1);
                         $arr = mysqli_fetch_assoc($result);
                         return $arr['总数'];
                    }
                    // var_dump(page($pagenum,$pagesize));
                    foreach(page($pagenum,$pagesize) as $key => $value){
                        echo "<tr>";
                        foreach($value as $item => $itemvalue){
                            echo "<td>".$itemvalue."</td>";
                        }
                            echo "<td>
                                <a class='sui-btn btn-small btn-warning' href='kecheng-edit.php?kid={$itemvalue}'>修改</a>
                                <a class='sui-btn btn-small btn-warning' href='kecheng-del.php?kid={$itemvalue}'>删除</a>
                            </td>";
                        
                        echo "</tr>";
                    }
                    /* if( mysqli_num_rows($result)>0 ){
                        while ($arr = mysqli_fetch_assoc($result)){
                            echo "<tr>
                                <td>{$arr['课程编号']}</td>
                                <td>{$arr['课程名称']}</td>
                                <td>{$arr['学习时长']}</td>
                                <td>
                                    <a class='sui-btn btn-small btn-warning' href='kecheng-edit.php?kid={$arr['课程编号']}'>修改</a>
                                    <a class='sui-btn btn-small btn-warning' href='kecheng-del.php?kid={$arr['课程编号']}'>删除</a>
                                </td>
                            </tr>";
                        }
                    }else{
                        echo "暂没有记录";
                    } */
                ?>
            </table>
            <div>总计：<?php echo $allnums; ?>条  
                <a href="?pagenum=1">首页</a>
                <a href="?pagenum=<?php echo $pagenum-1?>">上一页</a>
                <?php
                    for($i=1; $i<=$maxpage; $i++){
                        echo "<a href='?pagenum={$i}'>{$i}</a>&nbsp;";
                    }
                ?>
                <a href="?pagenum=<?php echo $pagenum+1?>">下一页</a>
                <a href="?pagenum=<?php echo $maxpage?>">尾页</a>
            </div>
		  </div>
		</div>		
    </div>
<?php include("foot.php"); ?>